package com.telecomyt.jwportal.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toolbar;

import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;

import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.utils.DisplayUtil;

import static android.util.TypedValue.COMPLEX_UNIT_SP;


/**
 * 自定义ToolBar
 */
public class FraToolBar extends Toolbar {

    private View rootView;
    private ImageView iv_left, iv_right;
    private TextView tv_left, tv_title, tv_right;
    private String backText;//返回键信息
    private int titleTextViewColor;
    private View rightTextView;
    private LinearLayout ll_right, ll_root;

    public FraToolBar(Context context) {
        this(context, null);
    }

    public FraToolBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FraToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initAttr(context, attrs);
    }

    //初始化属性
    private void initAttr(Context context, AttributeSet attrs) {

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FraToolBar);
        backText = typedArray.getString(R.styleable.FraToolBar_backText);//返回键文字描述信息

        boolean isTvBackShow = typedArray.getBoolean(R.styleable.FraToolBar_isTvBackShow, true);
        boolean isTvBackTextShow = typedArray.getBoolean(R.styleable.FraToolBar_isTvBackTextShow, false);
        boolean isIvLeftShow = typedArray.getBoolean(R.styleable.FraToolBar_isIvLeftShow, false);
        boolean isRightIconShow = typedArray.getBoolean(R.styleable.FraToolBar_isRightIconShow, false);
        boolean isTitleTextShow = typedArray.getBoolean(R.styleable.FraToolBar_isTitleTextShow, true);
        boolean isRightTextViewShow = typedArray.getBoolean(R.styleable.FraToolBar_isShowRightTextView, true);

        String rightText = typedArray.getString(R.styleable.FraToolBar_rightText);

        int rightIcon = typedArray.getResourceId(R.styleable.FraToolBar_rightIcon, R.mipmap.icon_cancle);
        int backIcon = typedArray.getResourceId(R.styleable.FraToolBar_backIcon, R.mipmap.icon_back);

        int titleTextViewColor = typedArray.getColor(R.styleable.FraToolBar_titleTextViewColor, Color.WHITE);
        int rightTextViewColor = typedArray.getColor(R.styleable.FraToolBar_rightTextViewColor, Color.WHITE);
        int backgroundColor = typedArray.getColor(R.styleable.FraToolBar_backgroundColor, Color.WHITE);
        float titleTextViewSize = typedArray.getDimension(R.styleable.FraToolBar_titleTextViewSize,18);
        float leftTextViewSize = typedArray.getDimension(R.styleable.FraToolBar_leftTextViewSize,  15);
        float rightTextViewSize = typedArray.getDimension(R.styleable.FraToolBar_rightTextViewSize,  15);
        typedArray.recycle();

        setBackTextViewVisable(isTvBackShow);
        setIsBackTextVisable(isTvBackTextShow);
        setIvLeftVisable(isIvLeftShow);
        setRightIconVisable(isRightIconShow);
        setTitleTextVisable(isTitleTextShow);
        setRightTextViewVisible(isRightTextViewShow);

        setBackText(backText);
        setRightText(rightText);
        setRightIcon(rightIcon);
        setBackIconResource(backIcon);
        setBarBackgroundColor(backgroundColor);
        setTitleTextViewColor(titleTextViewColor);
        setTitleTextViewSize(titleTextViewSize);
        setLeftTextViewSize(leftTextViewSize);
        setRightTextViewSize(rightTextViewSize);
        setRightTextViewColor(rightTextViewColor);
    }

    /**
     * 初始化View
     */
    private void initView() {
        //title是ToolBar自己的属性，初始化构造方法的时候会先初始化toolbar，此时titleview为null，所以不能显示
        if (rootView == null) {
            rootView = inflate(getContext(), R.layout.view_common_title_layout, null);
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
//                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                layoutParams.height = getResources().getDimensionPixelSize(dip2px(48));
//                layoutParams.height = dip2px(48);
                View mView = rootView.findViewById(R.id.mView);
                mView.setVisibility(View.GONE);
//                mView.setLayoutParams(layoutParams);

//                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                layoutParams.height = getResources().getDimensionPixelSize(R.dimen.y48);
//                View mView = rootView.findViewById(R.id.mView);
//                mView.setLayoutParams(layoutParams);
            }
            ll_root = rootView.findViewById(R.id.ll_root);
            iv_left = (ImageView) rootView.findViewById(R.id.iv_left);
            iv_right = (ImageView) rootView.findViewById(R.id.iv_right);
            tv_left = (TextView) rootView.findViewById(R.id.tv_left);
            ll_right = (LinearLayout) rootView.findViewById(R.id.ll_right);
            tv_title = (TextView) rootView.findViewById(R.id.tv_title);
            tv_right = (TextView) rootView.findViewById(R.id.tv_right);

            tv_right.setTextSize(COMPLEX_UNIT_SP, 14);
            this.addView(rootView);
        }
    }

    public int dip2px(float dipValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    @Override
    public void setTitle(@StringRes int resId) {
        setTitle(getResources().getString(resId));
    }

    @Override
    public void setTitle(CharSequence title) {
        ////title是ToolBar自己的属性，初始化构造方法的时候会先初始化toolbar，此时titleview为null，所以不能显示
        initView();
        tv_title.setText(TextUtils.isEmpty(title) ? "" : title);

    }

    /**
     * 设置返回键文字描述信息
     *
     * @param backStr
     */
    public void setBackText(String backStr) {
        tv_left.setText(TextUtils.isEmpty(backStr) ? "" : backStr);
    }

    /**
     * 设置返回键是否显示
     */
    public void setBackTextViewVisable(boolean b) {
        tv_left.setVisibility(b ? View.VISIBLE : View.GONE);
    }

    /**
     * 返回键默认是不显示文字，设置是否显示需要文字
     */
    public void setIsBackTextVisable(boolean b) {
        if (!b) {
            tv_left.setText("");
        } else {
            tv_left.setText(TextUtils.isEmpty(backText) ? "返回" : backText);
        }
    }

    /**
     * 设置左边的ImageView是否显示
     */
    public void setIvLeftVisable(boolean b) {
        iv_left.setVisibility(b ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置右边的ImageView是否显示
     */
    public void setRightIconVisable(boolean b) {
        iv_right.setVisibility(b ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置标题是否显示
     */
    public void setTitleTextVisable(boolean b) {
        tv_title.setVisibility(b ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置左边按钮的图标
     */
    public void setLeftIcon(int res) {
        iv_left.setImageResource(res);
    }

    /**
     * 设置右边按钮的图标
     */
    public void setRightIcon(int res) {
        iv_right.setImageResource(res);
    }

    public ImageView getMenuImageView() {
        return iv_left;
    }

    public ImageView getRightIconView() {
        return iv_right;
    }

    public TextView getBackTextView() {
        return tv_left;
    }

    public TextView getTitleTextView() {
        return tv_title;
    }

    /**
     * 设置菜单按钮的点击监听
     */
    public void setMenuOnClickListener(OnClickListener listener) {
        iv_left.setOnClickListener(listener);
    }

    public void setLeftFinish(final Activity activity) {
        tv_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
    }

    /**
     * 返回键的点击监听
     */
    public void setBackOnClickListener(OnClickListener listener) {
        tv_left.setOnClickListener(listener);
    }

    /**
     * 设置右边按钮的点击事件
     */
    public void setRightTextViewClickListener(OnClickListener listener) {
        tv_right.setOnClickListener(listener);
    }

    /**
     * 设置右边图标的点击监听
     */
    public void setRightIconOnCLickListener(OnClickListener listener) {
        ll_right.setOnClickListener(listener);
    }

    /**
     * 设置title的颜色
     */
    public void setTitleTextViewColor(int titleTextViewColor) {
        tv_title.setTextColor(titleTextViewColor);
    }

    /**
     * 设置title的字体大小
     */
    public void setTitleTextViewSize(float titleTextViewSize) {
        tv_title.setTextSize(titleTextViewSize);
    }

    /**
     * 设置left的字体大小
     */
    public void setLeftTextViewSize(float leftTextViewSize) {
        tv_left.setTextSize(leftTextViewSize);
    }

    /**
     * 设置Right的字体大小
     */
    public void setRightTextViewSize(float rightTextViewSize) {
        tv_right.setTextSize(rightTextViewSize);
    }

    /**
     * 设置右边的按钮是否可见
     */
    public void setRightTextViewVisible(boolean rightTextViewVisible) {
        tv_right.setVisibility(rightTextViewVisible ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置右边的描述文字
     */
    public void setRightText(String rightText) {
        tv_right.setText(rightText);
    }

    /**
     * 设置右边文字的颜色
     */
    public void setRightTextViewColor(int rightTextViewColor) {
        tv_right.setTextColor(rightTextViewColor);
    }

    /**
     * 设置右边文字的颜色
     */
    public void setBarBackgroundColor(int color) {
        ll_root.setBackgroundColor(color);
    }

    /**
     * 设置返回键图片
     */
    public void setBackIconResource(int backIconResource) {
        Drawable drawable = getResources().getDrawable(backIconResource);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tv_left.setCompoundDrawables(drawable, null, null, null);
    }

    public TextView getRightTextView() {
        return tv_right;
    }

    /**
     * 设置左边TextView的Drawable资源
     */
    public void setLeftTextViewDrawableResources(int drawableResource) {
        Drawable leftDrawable = getResources().getDrawable(drawableResource);
        leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
        tv_left.setCompoundDrawables(leftDrawable, null, null, null);
    }

    /**
     * 设置右边TextView的Drawable资源
     */
    public void setRightTextViewDrawableResources(int drawableResource) {
        Drawable rightDrawable = getResources().getDrawable(drawableResource);
        rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
        tv_right.setCompoundDrawables(rightDrawable, null, null, null);
    }

    public void setRightSize() {
        iv_right.setScaleType(ImageView.ScaleType.CENTER);
        ViewGroup.LayoutParams params = iv_right.getLayoutParams();
        params.height = 60;
        params.width = 100;
        iv_right.setLayoutParams(params);
    }

    public void setRightTextViewClickable(boolean isClickable) {
        tv_right.setEnabled(isClickable);
    }

    public void setRightTextVisibility(int visibility) {
        tv_right.setVisibility(visibility);
    }
}
